PostgreSQL 全文搜索 解析器
1 背景知识
解析器负责把文档文本划分成 记号
并且标识每一个 记号
的类型,类型由解析器本身定义。简单来说就是将文本拆分为 记号
。目前PostgreSQL只提供了一种解析器,称为内建 解析器
。
内建解析器被称为 pg_catalog.default
。它识别 23 种记号类型,如所示。
2 默认解析器的记号类型
别名 | 描述 | 例子 |
---|---|---|
asciiword |
单词,所有 ASCII 字母 | elephant |
word |
单词,所有字母 | mañana |
numword |
单词,字母和数字 | beta1 |
asciihword |
带连字符的单词,所有 ASCII | up-to-date |
hword |
带连字符的单词,所有字母 | lógico-matemática |
numhword |
带连字符的单词,字母和数字 | postgresql-beta1 |
hword_asciipart |
带连字符的单词部分,所有 ASCII | postgresql in the context postgresql-beta1 |
hword_part |
带连字符的单词部分,所有字母 | lógico or matemática in the context lógico-matemática |
hword_numpart |
带连字符的单词部分,字母和数字 | beta1 in the context postgresql-beta1 |
email |
Email 地址 | foo@example.com |
protocol |
协议头部 | http:// |
url |
URL | example.com/stuff/index.html |
host |
主机 | example.com |
url_path |
URL 路径 | /stuff/index.html , in the context of a URL |
file |
文件或路径名 | /usr/local/foo.txt , if not within a URL |
sfloat |
科学记数法 | -1.234e56 |
float |
十进制记数法 | -1.234 |
int |
有符号整数 | -1234 |
uint |
无符号整数 | 1234 |
version |
版本号 | 8.3.0 |
tag |
XML 标签 | <a href="dictionaries.html"> |
entity |
XML 实体 | & |
blank |
空格符号 | (其他不识别的任意空白或标点符号) |
Note
解析器的一个“字母”的概念是由数据库的 lc_ctype
设置决定的。只包含基本 ASCII 字母的词
被报告为一个单独的记号类型,因为有时可以用来区别它们。在大部分欧洲语言中,记号类型 word
和 asciiword
应该被同样对待。email
不支持 RFC 5322 定义的所有合法 email 字符。具体来说,对 email 用户名被支持的非字母数字字符只有句点、破折号和下划线
3 英文单词
SELECT alias, description, token FROM ts_debug('foo-bar-beta1');
alias | description | token
-----------------+------------------------------------------+---------------
numhword | Hyphenated word, letters and digits | foo-bar-beta1
hword_asciipart | Hyphenated word part, all ASCII | foo
blank | Space symbols | -
hword_asciipart | Hyphenated word part, all ASCII | bar
blank | Space symbols | -
hword_numpart | Hyphenated word part, letters and digits | beta1
(6 rows)
4 网页地址
SELECT alias, description, token FROM ts_debug('http://example.com/stuff/index.html');
alias | description | token
----------+---------------+------------------------------
protocol | Protocol head | http://
url | URL | example.com/stuff/index.html
host | Host | example.com
url_path | URL path | /stuff/index.html
(4 rows)
5 中文单词
SELECT alias, description, token FROM ts_debug('我的博客是:https://www.aming.tech');
alias | description | token
----------+-------------------+----------------
word | Word, all letters | 我的博客是
blank | Space symbols | :
protocol | Protocol head | https://
host | Host | www.aming.tech
(4 rows)
Note
initdb: 无法为本地化语言环境"zh_CN.UTF-8"找到合适的文本搜索配置。